﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using DataAccess;

namespace Business
{
    public class LoaiHang
    {
        
        public int MaLoai { get; set; }
        public string MaLoaiHienThi
        {
            get
            {
                return "LH" + MaLoai;
            }
        }
        public string TenLoai { get; set; }
        

        
        public LoaiHang(int maLoai, string tenLoai)
        {
            this.MaLoai = maLoai;
            this.TenLoai = tenLoai;
        }

        public LoaiHang(string tenLoai)
        {
            this.TenLoai = tenLoai;
        }

        public LoaiHang(string maloaihienthi, string tenloai)
        {
            this.MaLoai = Int16.Parse(maloaihienthi.Substring(2));
            this.TenLoai = tenloai;
        }
        

       
        public static void ThemMoi(LoaiHang nhapObject) {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

            DataAccess.LoaiHang tamObject = new DataAccess.LoaiHang();
            tamObject.TenLoai = nhapObject.TenLoai;

            db.LoaiHangs.InsertOnSubmit(tamObject);
            db.SubmitChanges();
        }

        public static void CapNhat(LoaiHang nhapObject) 
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

            var tamObject = (from p in db.LoaiHangs
                                    where p.MaLoai == nhapObject.MaLoai
                                    select p).Single();

            tamObject.TenLoai = nhapObject.TenLoai;
            db.SubmitChanges();
        }

        public static List<LoaiHang> LayDanhSach() 
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

            return (from p in db.LoaiHangs
                   select new LoaiHang(p.MaLoai, p.TenLoai)).ToList();
        }
        
    }
}
